//
//  BHSportCreatePlanTableViewCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/16.
//

import UIKit

class BHSportCreatePlanTableViewCell: UITableViewCell {
    
    var textViewDidEndEditingBlock:((String) -> Void)?
    
    @IBOutlet weak var place_label: UILabel!
    
    @IBOutlet weak var text_view: UITextView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
       
        
        self.text_view.delegate = self
        
        self.place_label.addViewAction {[weak self] (_) in
            self?.text_view.becomeFirstResponder()
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

extension BHSportCreatePlanTableViewCell: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        self.place_label.isHidden = !textView.text.isEmpty
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        self.textViewDidEndEditingBlock?(textView.text ?? "")
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
        }
        return true
    }
}
